﻿#region Copyright (c) 2000-2008 Developer Express Inc.
/*
{*******************************************************************}
{                                                                   }
{       Developer Express .NET Component Library                    }
{       AgDataGrid                                 }
{                                                                   }
{       Copyright (c) 2000-2008 Developer Express Inc.              }
{       ALL RIGHTS RESERVED                                         }
{                                                                   }
{   The entire contents of this file is protected by U.S. and       }
{   International Copyright Laws. Unauthorized reproduction,        }
{   reverse-engineering, and distribution of all or any portion of  }
{   the code contained in this file is strictly prohibited and may  }
{   result in severe civil and criminal penalties and will be       }
{   prosecuted to the maximum extent possible under the law.        }
{                                                                   }
{   RESTRICTIONS                                                    }
{                                                                   }
{   THIS SOURCE CODE AND ALL RESULTING INTERMEDIATE FILES           }
{   ARE CONFIDENTIAL AND PROPRIETARY TRADE                          }
{   SECRETS OF DEVELOPER EXPRESS INC. THE REGISTERED DEVELOPER IS   }
{   LICENSED TO DISTRIBUTE THE PRODUCT AND ALL ACCOMPANYING .NET    }
{   CONTROLS AS PART OF AN EXECUTABLE PROGRAM ONLY.                 }
{                                                                   }
{   THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      }
{   FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        }
{   COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       }
{   AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  }
{   AND PERMISSION FROM DEVELOPER EXPRESS INC.                      }
{                                                                   }
{   CONSULT THE END USER LICENSE AGREEMENT FOR INFORMATION ON       }
{   ADDITIONAL RESTRICTIONS.                                        }
{                                                                   }
{*******************************************************************}
*/
#endregion Copyright (c) 2000-2008 Developer Express Inc.

using System.Windows.Controls;
using System.Windows;
using DevExpress.AgDataGrid.Internal;
using System.Windows.Input;
using System.Windows.Shapes;
using System;
using System.Windows.Media;
using DevExpress.AgDataGrid.Data;
namespace DevExpress.AgDataGrid {
	public interface IGroupPanelDropElementOwner {
		void AcceptDragHeader(IDragableObject dragObject);
		int GroupedColumnCount { get; }
		double DefaultGroupPanelWidth { get; }
		FrameworkElement DragArrows { get; }
	}
	public class AgGroupStackPanel : AgIndentStackPanel, IDropableObject {
		IGroupPanelDropElementOwner owner;
		public AgGroupStackPanel()  
			: base(Orientation.Horizontal) {
		}
		protected IGroupPanelDropElementOwner Owner {
			get {
				if(owner == null) {
					owner = FindOwner();
				}
				return owner;
			}
		}
		protected virtual IGroupPanelDropElementOwner FindOwner() {
			UIElement parent = Parent as UIElement;
			while(parent != null) {
				IGroupPanelDropElementOwner owner = parent as IGroupPanelDropElementOwner;
				if(owner != null) return owner;
				FrameworkElement elem = parent as FrameworkElement;
				if(elem == null) break;
				parent = VisualTreeHelper.GetParent(elem) as UIElement;
			}
			return null;
		}
		protected override Size MeasureSortedChildrenOverride(Size availableSize, System.Collections.Generic.IList<UIElement> sortedChildren) {
			Size res = base.MeasureSortedChildrenOverride(availableSize, sortedChildren);
			if (Owner != null && double.IsNaN(res.Width)) res.Width = Owner.DefaultGroupPanelWidth;
			return res;
		}
		#region IDropableObject Members
		bool IDropableObject.CanAccept() { return Owner != null && Owner.GroupedColumnCount == 0; } 
		bool IDropableObject.CanAccept(IDragableObject dragObject, Point position) {
			if(Owner == null) return false;
			AgDataGridColumnHeader dragHeader = dragObject as AgDataGridColumnHeader;
			if(dragHeader == null || dragHeader.Owner != Owner) return false;
			if(!dragHeader.Column.CanUserGroup) return false;
			return Owner.GroupedColumnCount == 0;
		}
		Rect IDropableObject.GetThumbRect(Point position) {
			return new Rect(0, 0, 10, ActualHeight);
		}
		void IDropableObject.AcceptDrag(IDragableObject dragObject, Point position) {
			if(Owner == null) return;
			Owner.AcceptDragHeader(dragObject);
		}
		FrameworkElement IDropableObject.GetThumbObject() {
			return Owner.DragArrows;
		}
		#endregion
	}
}
